package com.jcli.util._获取并保存窗体大小;

import javax.swing.*;
import java.awt.*;
import java.io.*;

/**
 *
 * @author li
 * @version 1.3
 * @date 2024/2/1
 */
public class WindowSizeManager {

    private static final String SIZE_FILE = "size/size.txt";

    public static void saveWindowSize(JFrame frame) {
        try (PrintWriter writer = new PrintWriter(new FileWriter(SIZE_FILE))) {
            writer.println(frame.getWidth());
            writer.println(frame.getHeight());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void applyWindowSize(JFrame frame) {
        try (BufferedReader reader = new BufferedReader(new FileReader(SIZE_FILE))) {
            int width = Integer.parseInt(reader.readLine());
            int height = Integer.parseInt(reader.readLine());
            Dimension size = new Dimension(width, height);
            frame.setSize(size);
            frame.setLocationRelativeTo(null); // 居中显示
        } catch (IOException | NumberFormatException e) {
            // 如果文件不存在或者格式错误，可以不进行处理

        }
    }
}
